﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using YellowMap.View.Control;
using System.Windows.Media;
using YellowMap.View.Page;

namespace YellowMap.View.Control
{
    public partial class HistoryListItem : UserControl
    {
        public static HistoryListItem SelectedHistoryListItem { get; set; }
        public static HistoryListItem PressedItem { get; set; }
        double menuHeight = 170;
        public HistoryListItem()
        {
            InitializeComponent();
            //Loaded += HistoryListItem_Loaded;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] != Visibility.Visible)
            {
                txtName.Foreground = new SolidColorBrush(Colors.Black);
                txtAddress.Foreground = new SolidColorBrush(Colors.Black);
            }
            else
            {
                txtName.Foreground = new SolidColorBrush(Colors.White);
                txtAddress.Foreground = new SolidColorBrush(Colors.White);
            }
            //txtAddress.Foreground = new SolidColorBrush((App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush).Color);
            this.MouseEnter += HistoryListItem_MouseEnter;
            this.MouseLeftButtonDown += HistoryListItem_MouseLeftButtonDown;
           
            menu.Opened += menu_Opened;

        }

        double mouseY;
        void HistoryListItem_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            mouseY = e.GetPosition(this).Y;
            // throw new NotImplementedException();
        }

        void menu_Opened(object sender, RoutedEventArgs e)
        {
            var listTop = VisualTreeHelper.FindElementsInHostCoordinates(new Rect(0, 0, 400, 400), HistoryPage.Intance);
            if (listTop.Contains(this))
            {
                menu.VerticalOffset = ActualHeight - mouseY;
            }
            else
            {
                menu.VerticalOffset = -menuHeight - mouseY ;
            }
            //throw new NotImplementedException();
        }

        private void HistoryListItem_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            PressedItem = this;
        }

       
        public static Color ConvertColorFromHexString(string hexaColor)
        {
            hexaColor = hexaColor.Trim(new char[] { '#' });
            if (hexaColor.Length == 6)
            {
                hexaColor += "ff";
            }

            return Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(6, 2), 16),
                    Convert.ToByte(hexaColor.Substring(0, 2), 16),
                    Convert.ToByte(hexaColor.Substring(2, 2), 16),
                    Convert.ToByte(hexaColor.Substring(4, 2), 16));
        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            AppViewModel.Instance.SettingVM.SelectedHistory = HistoryListItem.PressedItem.DataContext as History;
            MainPage.COMMAND = MainPage_Command.SHOW_HISTORY;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            if ((MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled == false)
            {
                (MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = true;
            }
            HistoryPage.Intance.NavigationService.GoBack();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {

            //[pkhuy]
            if (AppViewModel.Instance.SettingVM.SearchFrom == Constant.SEARCHFROM_CHOOSEONMAP)
            {
                AppViewModel.Instance.SettingVM.MapLattitude = MainPage.Instance.CenterLocation.Latitude;
                AppViewModel.Instance.SettingVM.MapLongitude = MainPage.Instance.CenterLocation.Longitude;
            }

            AppViewModel.Instance.SettingVM.SelectedHistory = HistoryListItem.PressedItem.DataContext as History;
            MainPage.COMMAND = MainPage_Command.SEARCH_HISTORY;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            if ((MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled == false)
            {
                (MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = true;
            }
            SearchPage.isSearchHistory = true;
            HistoryPage.Intance.NavigationService.GoBack();
        }
        
        //private void UserControl_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        //{
        //    IsSelected = true;
        //}
    }
}
